﻿using HomagChina.CCT.BaseUI.ViewModel;
using HomagChina.CCT.BaseUI.DataModel;
using HomagChina.CCT.BaseUI.Router;
using HomagGroup.Base.UI;
using System.Collections.Generic;
using System.Windows;
using HomagChina.CCT.BaseUI.Views;
using System.Linq;
using System.Collections.ObjectModel;
using System;

namespace HomagGroup.BaseUI.Router
{
    public class RouterModel
    {
        public static MainWindowViewModel mainWindowViewModel;

        public static Dictionary<string, Lazy<RouterItemDataModel>> RouterDictionary;

        public RouterModel()
        {
            Application.Current.Properties["RouterMap"] = new Dictionary<string, Lazy<RouterItemDataModel>> {
                {"Home", new Lazy<RouterItemDataModel>(()=> new HomeRoute().HomeRouteModel) },
                {"Settings", new Lazy<RouterItemDataModel>(()=>new SettingsRoute().SettingsRouteModel)},

                {"ErrorMessage",new Lazy<RouterItemDataModel>(()=>new ErrorMessageRoute().ErrorMessageRouteModel)},
                {"Statistics",new Lazy<RouterItemDataModel>(()=> new StatisticsRoute().StatisticsRouteModel)},
                {"TransHistory", new Lazy<RouterItemDataModel>(()=>new TransHistoryRoute().TransHistoryRouteModel)},
            };
            RouterDictionary = (Dictionary<string, Lazy<RouterItemDataModel>>)Application.Current.Properties["RouterMap"];
        }

        public void start()
        {
            mainWindowViewModel = new MainWindowViewModel();
            MainWindow mainWindow = new MainWindow();
            mainWindow.DataContext = mainWindowViewModel;
            if (RouterDictionary.TryGetValue("Home", out var homeLazy))
            {
                var homeRoute = homeLazy.Value; // 确保这里不会抛出异常
                mainWindowViewModel.RouterItems.Add(homeRoute);
                mainWindowViewModel.FunctionKeyBarMenu = homeRoute.functionKeyBarItems;
                mainWindowViewModel.ContentElement = homeRoute.userControl;
            }
            mainWindow.Show();
        }
    }

    public class NavigateServices
    {
        private static NavigateServices instance;
        private static readonly object padlock = new object();

        public RouterItemDataModel UpdateBindDataContext(RouterItemDataModel routerInfo)
        {
            RouterItemDataModel newItem = null;
            switch (routerInfo.ID)
            {
                case "Home":
                    newItem = new HomeRoute().HomeRouteModel;
                    break;

                case "Settings":
                    newItem = new SettingsRoute().SettingsRouteModel;
                    break;

                case "ErrorMessage":
                    newItem = new ErrorMessageRoute().ErrorMessageRouteModel;
                    break;

                case "Statistics":
                    newItem = new StatisticsRoute().StatisticsRouteModel;
                    break;

                case "TransHistory":
                    newItem = new TransHistoryRoute().TransHistoryRouteModel;
                    break;

                default:
                    break;
            }
            return routerInfo.keepalive ? null : newItem;
        }

        public void NavigateTo(string Id)
        {
            var routerInfo = RouterModel.RouterDictionary[Id].Value;
            MainWindowViewModel mainWindowViewModel = RouterModel.mainWindowViewModel;
            if (!mainWindowViewModel.RouterItems.Any(item => item.ID == routerInfo.ID) || routerInfo.ID == "Home")
            {
                if (routerInfo.ID != "Home") mainWindowViewModel.RouterItems.Add(routerInfo);

                if (!routerInfo.keepalive)
                {
                    var viewModel = UpdateBindDataContext(routerInfo).ViewModel;
                    routerInfo.userControl.DataContext = viewModel;
                    routerInfo.ViewModel = viewModel;
                }
                mainWindowViewModel.ContentElement = null;
                mainWindowViewModel.ContentElement = routerInfo.userControl;
                mainWindowViewModel.FunctionKeyBarMenu = routerInfo.functionKeyBarItems;
            };
        }

        public void LanguageManagerCultureChanged(object sender, System.EventArgs e)
        {
            new RouterModel();
            MainWindowViewModel mainWindowViewModel = RouterModel.mainWindowViewModel;
            var UpdateRouters = new ObservableCollection<RouterItemDataModel>();
            foreach (var item in mainWindowViewModel.RouterItems)
            {
                UpdateRouters.Add(RouterModel.RouterDictionary[item.ID].Value);
            }
            mainWindowViewModel.RouterItems.Clear();
            mainWindowViewModel.RouterItems.AddRange(UpdateRouters);
            mainWindowViewModel.ContentElement = RouterModel.RouterDictionary[mainWindowViewModel.RouterItems.Last().ID].Value.userControl;
            mainWindowViewModel.FunctionKeyBarMenu = RouterModel.RouterDictionary[mainWindowViewModel.RouterItems.Last().ID].Value.functionKeyBarItems;
        }

        public static NavigateServices Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new NavigateServices();
                    }
                }
                return instance;
            }
        }
    }
}